package cn.edu.tju.se.records.dao.hibernate;

import java.util.List;

import cn.edu.tju.se.base.service.ReadProperties;
import org.springframework.stereotype.Repository;

import cn.edu.tju.se.base.dao.hibernate.BaseDaoImpl;
import cn.edu.tju.se.records.dao.PhotoDao;
import cn.edu.tju.se.records.domain.Photo;
@Repository
public class PhotoDaoImpl extends BaseDaoImpl<Photo> implements PhotoDao{
	private final String GET_PHOTO_BY_PATIENTID = "from Photo p where p.patientid = ? order by p.photodate asc";
    private final String GET_PHOTO_BY_ID = "from Photo p where p.id = ?";
    private final String GET_PHOTOS_BY_DATE = "from Photo p where p.photodate = ? and p.patientid = ?";
	
	@SuppressWarnings("unchecked")
	public List<Photo> getPhotoByPatientId(String PatientId){
        List<Photo> photo = (List<Photo>)find(GET_PHOTO_BY_PATIENTID,PatientId);
        if (photo.size() == 0) {
            return null;
        }else{
            return photo;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Photo> getPhotoById(int id){
        List<Photo> photo = (List<Photo>)find(GET_PHOTO_BY_ID,id);
        if (photo.size() == 0) {
            return null;
        }else{
            return photo;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Photo> getPhotoByDate(String patientid,String photodate){
        List<Photo> photo = (List<Photo>)find(GET_PHOTOS_BY_DATE,photodate,patientid);
        if (photo.size() == 0) {
            return null;
        }else{
            return photo;
        }
    }

    @SuppressWarnings("unchecked")
    public void removePhotosbyPatientid(String PatientId){
        List<Photo> photos = (List<Photo>)find(GET_PHOTO_BY_PATIENTID,PatientId);
        if(photos!=null){
            for (Photo photo:photos)
            {
                remove(photo);
            }
        }

    }

    public void removePhotosbyDate(String patientid,String photodate){
        List<Photo> photos = (List<Photo>)find(GET_PHOTOS_BY_DATE,photodate,patientid);
        if(photos!=null){
            for (Photo photo:photos)
            {
                remove(photo);
            }
        }

    }

    @SuppressWarnings("unchecked")
    public String getPhotoRoot(){
        ReadProperties readproperties = new ReadProperties();
        return readproperties.getUploadPath();
    }
}
